package com.ruoyi.business.device.controller;

import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.device.domain.Device;
import com.ruoyi.business.device.service.IDeviceService;

import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备信息Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/business/device")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }



    /**
     * 获取设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:device:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
    {
        return success(deviceService.selectDeviceByDeviceId(deviceId));
    }

    /**
     * 新增设备信息
     */
    @PreAuthorize("@ss.hasPermi('business:device:add')")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Device device)
    {
        if ("1".equals(deviceService.checkDeviceRelayCodeUnique(device)))
        {
            return error("新增设备'" + device.getDeviceName() + "'失败，设备中继编码已存在");
        }
        device.setCreateBy(getUsername());
        return toAjax(deviceService.insertDevice(device));
    }

    /**
     * 修改设备信息
     */
    @PreAuthorize("@ss.hasPermi('business:device:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Device device)
    {
        if ("1".equals(deviceService.checkDeviceRelayCodeUnique(device)))
        {
            return error("修改设备'" + device.getDeviceName() + "'失败，设备中继编码已存在");
        }
        device.setUpdateBy(getUsername());
        return toAjax(deviceService.updateDevice(device));
    }

    /**
     * 删除设备信息
     */
    @PreAuthorize("@ss.hasPermi('business:device:remove')")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds)
    {
        return toAjax(deviceService.deleteDeviceByDeviceIds(deviceIds));
    }

    /**
     * 根据设备中继编码查询设备信息
     */
    @PreAuthorize("@ss.hasPermi('business:device:query')")
    @GetMapping("/relay/{deviceRelayCode}")
    public AjaxResult getInfoByRelayCode(@PathVariable("deviceRelayCode") String deviceRelayCode)
    {
        return success(deviceService.selectDeviceByDeviceRelayCode(deviceRelayCode));
    }

    /**
     * 更新设备连接状态
     */
    @PreAuthorize("@ss.hasPermi('business:device:edit')")
    @Log(title = "设备连接状态", businessType = BusinessType.UPDATE)
    @PutMapping("/connection/{deviceId}/{connectionStatus}")
    public AjaxResult updateConnectionStatus(@PathVariable("deviceId") Long deviceId, 
                                           @PathVariable("connectionStatus") String connectionStatus)
    {
        return toAjax(deviceService.updateDeviceConnectionStatus(deviceId, connectionStatus));
    }

    /**
     * 更新设备最后通信时间
     */
    @PreAuthorize("@ss.hasPermi('business:device:edit')")
    @Log(title = "设备通信时间", businessType = BusinessType.UPDATE)
    @PutMapping("/commTime/{deviceId}")
    public AjaxResult updateLastCommTime(@PathVariable("deviceId") Long deviceId)
    {
        return toAjax(deviceService.updateDeviceLastCommTime(deviceId));
    }
}
